package com.hw.one.transpot.fileupload.job;

import com.hw.one.collect.cache.CacheService;
import com.hw.one.collect.fileupload.entity.UploadData;
import com.hw.one.collect.fileupload.job.CompressJob;
import com.hw.one.transpot.fileupload.util.FileTransportClientUtil;
import com.hw.one.collect.job.Job;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.List;

/**
 * @Class UploadJob：上传处理job
 * @Author guoshenzhen
 * @Date 2018/3/14  15:59
 */
@Component
public class UploadDealJob extends Job {
    private Logger log = Logger.getLogger(this.getClass());
    @Override
    public void start(){
        log.debug(this.getClass().getSimpleName() + ".start--->>>transport starting...");
        deal();
        log.debug(this.getClass().getSimpleName() + ".start--->>>transport ended...");
    }

    public void deal() {
        List<Object> list = CacheService.list;
        if(!list.isEmpty()){
            synchronized (CacheService.list) {
                Iterator<Object> iterator = CacheService.list.iterator();
                while(iterator.hasNext()){
                    Object obj = iterator.next();
                    if(obj instanceof UploadData){
                        UploadData data = (UploadData) obj;
                        new FileTransportClientUtil().connect(FileTransportClientUtil.FILE_IP, FileTransportClientUtil.FILE_PORT, data.getFilePath());
                        iterator.remove();
                    }
                }
            }
        }else {
            log.debug("--------->>>>>>UploadDealJob list is empty2");
        }
    }

//    public static void main(String[] args){
//        long start = System.currentTimeMillis();
//        new CompressJob().start();
//        new UploadDealJob().start();
//        System.out.println(System.currentTimeMillis() - start);
//    }
}
